<?php

class PicDocAction extends CommonAction {
	
    public function index(){


        $this->assign("picDocTableData", M('Pdoc')->where(array('pdoc_isdel'=>'0'))->select());

    	$this->display();

    }

    public function addPage()
    {
        # code...
        $this->assign("pdoc_sn", "SN_".$this->createSN());
        $this->assign("catagory", D('Dictionary')->getPicDocCatagory());

        $this->display();
    }
    
    public function updatePage(){
        
        $pdoc_id = I('pdoc_id', '0');
        $pdoc = M('Pdoc')->where(array('pdoc_id'=>$pdoc_id))->find();
        $this->assign("pdoc", $pdoc);
        $this->assign("catagory", D('Dictionary')->getPicDocCatagory());
        
        $this->display();
    }

    public function addCatagoryPage(){
        
        $this->assign("catagory", D('Dictionary')->getPicDocCatagory());
        
        $this->display();
    }
    
    public function preViewPage(){
        $pdoc_id = I('pdoc_id', '0');
        $pdoc = M('Pdoc')->where(array('pdoc_id'=>$pdoc_id))->find();
        if(!$pdoc){
            $this->error("文章不存在！");
            exit();
        }
        $names = getDocFileNames(UPLOAD_PATH.$pdoc['pdoc_sn']);
        //重新排序一下
        usort($names, "strnatcmp");
        for ($i=0; $i < count($names); $i++) { 
            $pdoc['imgs'][$i] = $pdoc['pdoc_sn'].'/'.$names[$i];
        }

        $this->assign("pdoc", $pdoc);
        $this->assign("imgs", $pdoc['imgs']);

        $this->display();
        
    }
    
    

    public function createSN(){
        $num = mt_rand(10000, 99999);
        if( M('Pdoc')->where(array('pdoc_sn' => $num))->find() ){
            $num = mt_rand(100000, 999999);
        }
        return $num;
    }

    public function savePicDoc()
    {

        $db_pdoc = M('Pdoc');
        if($db_pdoc->create()){
            $cur_pic_upload_path = "./Upload/".I('pdoc_sn','SN_0');
            $db_pdoc->pdoc_save_path = $cur_pic_upload_path;
            $db_pdoc->pdoc_createtime = date("Y-m-d H:i:s", time());
            
            $res = $db_pdoc->where(array('pdoc_name'=>$db_pdoc->pdoc_name))->find();
            if($res){
                delDirAndFile($cur_pic_upload_path, true);
                $this->ajaxReturn(array('status' => -1, 'info' => '新增失败！已存在同名文档，编号：'.$res['pdoc_sn'] ,'data' => null, 'jumpUrl' => U('PicDoc/index') ));
                return;
            }
            
            if($db_pdoc->add()){
                $this->ajaxReturn(array('status' => 1, 'info' => '新增成功！', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
            }else{
                delDirAndFile($cur_pic_upload_path, true);
                $this->ajaxReturn(array('status' => 0, 'info' => '新增失败', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
            }
        }

    }
    
    public function updatePicDoc(){
        $db_pdoc = M('Pdoc');
        if($db_pdoc->create()){
            $db_pdoc->pdoc_createtime = date("Y-m-d H:i:s", time());
            if($db_pdoc->save()){
                $this->ajaxReturn(array('status' => 1, 'info' => '更新成功！', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
            }else{
                $this->ajaxReturn(array('status' => 0, 'info' => '更新失败', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
            }
        }
    }

    public function modifyPicDoc()
    {
        # code...
        $db_pdoc = M('Pdoc');
        if($db_pdoc->create()){
            $res = $db_pdoc->where(array('pdoc_name'=>$db_pdoc->pdoc_name))->find();
            if($res){
                $this->ajaxReturn(array('status' => -1, 'info' => '更新失败！已存在同名文档，编号：'.$res['pdoc_sn'] ,'data' => null, 'jumpUrl' => U('PicDoc/index') ));
                return;
            }
            if($db_pdoc->save()){
                $this->ajaxReturn(array('status' => 1, 'info' => '更新成功！', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
            }else{
                $this->ajaxReturn(array('status' => 0, 'info' => '更新失败', 'data' => null, 'jumpUrl' => null ));
            }
        }
    }

    public function delPicDoc()
    {

        $pdoc_id = I("post.pdoc_id", "-1");
        $pdoc_sn = I("post.pdoc_sn", "SN_0");

        //这里千万不要写错！！！
        $upload_root =  "./Upload/";
        $cur_pic_upload_path = $upload_root.$pdoc_sn;

        //调用这个方法一定要非常小心，路径千万不要写错了，否则后果很严重！
        if(M('Pdoc')-> where(array('pdoc_id'=>$pdoc_id))->setField('pdoc_isdel', 1) 
                && delDirAndFile($cur_pic_upload_path, true)){
            $this->ajaxReturn(array('status' => 1, 'info' => '删除成功', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
        }else{
            $this->ajaxReturn(array('status' => 0, 'info' => '删除失败', 'data' => null, 'jumpUrl' => null ));
        }
       // echo $pdoc_sn;
    }


    public function delPicFiles()
    {
        # code...
        $pdoc_sn = I("post.pdoc_sn", "SN_0");
         //这里千万不要写错！！！
        $upload_root =  "./Upload/";
        $cur_pic_upload_path = $upload_root.$pdoc_sn;
        if( delDirAndFile($cur_pic_upload_path, true) ){
            $this->ajaxReturn(array('status' => 1, 'info' => '删除成功', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
        }
        else{
            $this->ajaxReturn(array('status' => 0, 'info' => '删除失败', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
        }

    }
    
    public function upload()
    {
        # code...
        import("ORG.Net.UploadFile");

        $pdoc_sn = I("get.pdoc_sn","SN_0");

        $upload = new UploadFile();

        // 上传文件保存路径
        $upload->savePath =  "./Upload/";
        //存在子目录
        $upload->autoSub = true;
        $upload->subType = 'custom';
        //文件保存路径下的目录，这里有乱码，所以不能输入中文
        $upload->subDir = $pdoc_sn;
        //上传文件名保持原文件名
        $upload->saveRule = 'keepnum';
        // 存在同名就覆盖
        $upload->uploadReplace = true;

        if($upload->upload()){
            $info = $upload->getUploadFileInfo();
            echo json_encode(array("status" => 1, "msg" => "success"));
        }else{
            echo json_encode(array( "status" => 0, "msg" => $upload->getErrorMsg() ));
        }
    }
    
    
    public function addCatagory(){
        $catagory_name = I('catagory_name', '');
        
        if( D('Dictionary')->addPicDocCatagory($catagory_name) ){
            $this->ajaxReturn(array('status' => 1, 'info' => '新增成功', 'data' => null, 'jumpUrl' => U('PicDoc/addCatagoryPage') ));
        }
        else
        {
            $this->ajaxReturn(array('status' => 0, 'info' => '删除失败', 'data' => null, 'jumpUrl' => U('PicDoc/addCatagoryPage') ));
        }
        
    }
    
    public function saveCatagoryConfig(){
        //是一个json数组对象的数据类型[{},{}]
        $catagoryInfoData = $this->_post('data','');
        D('Dictionary')->updatePicDocCatagory($catagoryInfoData);
        $this->ajaxReturn(array('status' => 1, 'info' => '更新成功', 'data' => null, 'jumpUrl' => U('PicDoc/index') ));
        
    }

}

?>